package com.qf.service.impl;

import com.qf.dao.AccountDao;
import com.qf.pojo.Account;
import com.qf.service.AccountService;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * @ClassName AccountServiceImpl
 * @Description TODO
 * @Author Windy
 * @Date 2022/8/31 10:14
 * @Version 1.0
 **/
@Transactional
public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;


    //使用set方法注入
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void transfer(String sourceName, String targetName, Double money) {
                //根据名称查询出转账用户
                Account sourceAccount = accountDao.findAccountByName(sourceName);
                //根据名称查询转入账用户
                Account targetAccount = accountDao.findAccountByName(targetName);
                //转出账户扣钱
                sourceAccount.setMoney(sourceAccount.getMoney() - money);
                //转入账户加钱
                targetAccount.setMoney(targetAccount.getMoney() + money);
                //更新转出账户
                accountDao.updateAccount(sourceAccount);
                int i = 1/0;
                //更新转入账户
                accountDao.updateAccount(targetAccount);

    }
}
